home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / iris.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  237 lines

  1. /*
  2.  * $Id: iris.c,v 0.91 1994/02/20 00:52:47 zhao Pre-Release $
  3.  *
  4.  *. This file is part of BIT shareware package. After the two weeks of
  5.  *  free evaluation period, you are encouraged (required) to register
  6.  *  your copy for a small registration fee, which is $35 for personal use
  7.  *  and $50 for commercial, government and institutional use.
  8.  *
  9.  *  Copyright(c) 1993, 1994 by T.C. Zhao.
  10.  *  All rights reserved.
  11.  *
  12.  *  Permission to use, copy, and distribute this software in its entirety
  13.  *  for non-commercial purposes is hereby granted, provided that the
  14.  *  above shareware and copyright notices and this permission notice
  15.  *  appear in all copies and their documentation.
  16.  *
  17.  *  This software may be modified for your own use, but modified versions
  18.  *  may not be distributed without prior consent of the author.
  19.  *
  20.  *  This software is provided "as is" without expressed or implied
  21.  *  warranty of any kind.
  22.  *
  23.  *.
  24.  * IRIS RGB stuff. Using  Paul Haeberli's library as found in ~4Dgifts.
  25.  *
  26.  */
  27. #if !defined(lint) && defined(F_ID)
  28. char *id_iris = "$Id: iris.c,v 0.91 1994/02/20 00:52:47 zhao Pre-Release $";
  29. #endif
  30.  
  31. #include "bit.h"
  32. #include "gl/image.h"
  33. #include "dmalloc.h"
  34.  
  35. /*************** local variable ***************************/
  36.  
  37. static IMAGE *iris_head;    /* IRIS RGB header info        */
  38. static long iris_rlines;    /* number of lines read so far */
  39. static const char *irisload = "Load_IRIS";
  40. static int iriserr;        /* to prevent excess error reporting */
  41.  
  42. /************ there is no prototyping in gl/image.h. do it here ****/
  43.  
  44. extern int i_seterror(void (*p) (const char *));
  45. extern int getrow(IMAGE *, unsigned short *, unsigned, unsigned);
  46. extern int putrow(IMAGE *, unsigned short *, unsigned, unsigned);
  47. extern int iclose(IMAGE *);
  48.  
  49. /**************************************************************
  50.  *    take over the error handling from imagelib.
  51.  **************************************************************/
  52.  
  53. static void
  54. err_func(const char *s)
  55. {
  56.     Bark(irisload, s);
  57.     iriserr++;
  58. }
  59.  
  60.  
  61. /*************************************************************
  62.  * get IRIS image type and  dimension
  63.  *************************************************************/
  64. int
  65. RGB_desc(IPTR a)
  66. {
  67.     i_seterror(err_func);
  68.  
  69.     iriserr = 0;
  70.     iris_head = iopen(a->ifile, "r", 0, 0, 0, 0, 0);
  71.  
  72.     if (!iris_head)
  73.       {
  74.       Bark("IRIS_desc", a->ifile);
  75.       return -1;
  76.       }
  77.  
  78.     a->w = iris_head->xsize;
  79.     a->h = iris_head->ysize;
  80.     if (iris_head->zsize >= 3)
  81.     a->type = T_RGBA;
  82.     else if (iris_head->zsize == 1)
  83.     a->type = T_GRAY;    /* not sure this is correct */
  84.     else
  85.     a->type = T_CMAP;    /* not sure about this either */
  86.  
  87.     set_iformat_info(a, (ISRLE(iris_head->type)) ? "RLE compressed" : "");
  88.     return iriserr > 1 ? -1 : 0;
  89. }
  90.  
  91. /***********************************************************
  92.  *    load all pixels by calling getrow repeatedly
  93.  ************************************************************/
  94. int
  95. RGB_load(IPTR im)
  96. {
  97.     register rgba_t *head;
  98.     register unsigned short *rbuf, *gbuf = 0, *bbuf = 0, *rs;
  99.     register size_t y;
  100.     unsigned short *rh = 0, *gh = 0, *bh = 0;
  101.     int err = 0;
  102.  
  103.     iris_rlines = progress_report("Loading IRIS...", im->h);
  104.     head = im->raster;
  105.     y = sizeof(unsigned short) * im->w;
  106.  
  107.     if (IS_RGBA(im))
  108.       {
  109.       if ((err = !(rh = rbuf = malloc(y)) ||
  110.            !(gh = gbuf = malloc(y)) ||
  111.            !(bh = bbuf = malloc(y))))
  112.         {
  113.         Bark(irisload, "malloc failed");
  114.         Free(rh);
  115.         Free(gh);
  116.         Free(bh);
  117.         return -1;
  118.         }
  119.       /* Load raster one row at a time and pack it on the fly */
  120.       for (y = err = 0; !err && y < im->h; y++)
  121.         {
  122.         err = (iriserr > 2) ||
  123.             (getrow(iris_head, (rbuf = rh), y, 0) < 0) ||
  124.             (getrow(iris_head, (gbuf = gh), y, 1) < 0) ||
  125.             (getrow(iris_head, (bbuf = bh), y, 2) < 0);
  126.  
  127.         for (rs = rh + im->w; rbuf < rs; rbuf++, gbuf++, bbuf++)
  128.           {
  129.               *head++ = Pack(*rbuf, *gbuf, *bbuf);
  130.           }
  131.         REPORT(y, iris_rlines);
  132.         }
  133.       free(rh);
  134.       free(gh);
  135.       free(bh);
  136.       }
  137.     else if (IS_RGBGRAY(im))
  138.       {
  139.       rh = rbuf = malloc(y);
  140.       for (y = err = 0; !err && y < im->h; y++)
  141.         {
  142.         err = (iriserr > 2) ||
  143.             (getrow(iris_head, (rbuf = rh), y, 0) < 0);
  144.         for (rs = rh + im->w; rbuf < rs; rbuf++, head++)
  145.           {
  146.               *head = Pack(*rbuf, *rbuf, *rbuf);
  147.           }
  148.         REPORT(y, iris_rlines);
  149.         }
  150.       free(rh);
  151.       }
  152.     else
  153.       {
  154.       Bark(irisload, "%s: Bad image type", im->ifile);
  155.       err = -1;
  156.       }
  157.     err = (iclose(iris_head) || err);
  158.     /*
  159.      * for all memory not malloc'ed directly by bit, should use Tfree in case
  160.      * malloc wrapper is used in bit
  161.      */
  162.     Tfree(iris_head);
  163.     remove_progress_report();
  164.     return y;
  165. }
  166. /****************************************************************
  167.  * write image in IRIS format to disk
  168.  *****************************************************************/
  169. int
  170. RGB_dump(IPTR im)
  171. {
  172.     register rgba_t *head;
  173.     register unsigned short *rbuf, *gbuf, *bbuf, *rs;
  174.     register size_t y;
  175.     unsigned short *rh, *gh = 0, *bh = 0;
  176.     int z;
  177.  
  178.  
  179.     z = IS_GRAY(im) ? 1 : 3;
  180.  
  181.     /* in case we write IRIS directly */
  182.     i_seterror(err_func);
  183.  
  184.     /* always write RLE compressed */
  185.     if (!(iris_head = iopen(im->ofile, "w", RLE(1), 3, im->w, im->h, z)))
  186.     return -1;
  187.  
  188.     iris_rlines = progress_report("Writing IRIS...", im->h);
  189.     head = im->raster;
  190.     y = sizeof(*rbuf) * im->w;
  191.     if (z == 3)
  192.       {
  193.       if (!(rh = rbuf = malloc(y)) ||
  194.           !(gh = gbuf = malloc(y)) ||
  195.           !(bh = bbuf = malloc(y)))
  196.         {
  197.         Bark("IRIS_dump", "malloc failed");
  198.         Free(rh);
  199.         Free(gh);
  200.         Free(bh);
  201.         return -1;
  202.         }
  203.  
  204.       /* get one row a time and call putrow to output */
  205.       for (y = 0; y < im->h; y++)
  206.         {
  207.         for (rs = rh + im->w; rbuf < rs; head++, rbuf++, gbuf++, bbuf++)
  208.           {
  209.               Unpack(*head, *rbuf, *gbuf, *bbuf);
  210.           }
  211.         putrow(iris_head, (rbuf = rh), y, 0);
  212.         putrow(iris_head, (gbuf = gh), y, 1);
  213.         putrow(iris_head, (bbuf = bh), y, 2);
  214.         REPORT(y, iris_rlines);
  215.         }
  216.       free(rh);
  217.       free(gh);
  218.       free(bh);
  219.       }
  220.     else
  221.       {                /* grayscale */
  222.       rh = rbuf = malloc(y);
  223.       for (y = 0; y < im->h; y++)
  224.         {
  225.         for (rs = rh + im->w; rbuf < rs; head++, rbuf++)
  226.             *rbuf = (*head & (PCMAX - 1));
  227.         putrow(iris_head, (rbuf = rh), y, 0);
  228.         REPORT(y, iris_rlines);
  229.         }
  230.       free(rh);
  231.       }
  232.     iclose(iris_head);
  233.     Tfree(iris_head);
  234.     remove_progress_report();
  235.     return 0;
  236. }
  237.